﻿using ConfExpert.Utility;
using DevExpress.XtraEditors;
using Extensions;
using System;
using System.Windows.Forms;

namespace ConfOrder.Articles
{
    public partial class ArticlePrices : XForm
    {
        public ArticlePrices()
        {
            InitializeComponent();
        }

        private void FabricPrices_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;
            gvFabric.Init("COGetUnitPrice", AppSession.UserInfo.CustomerId);
            gvFabric.MakeColumnInvisible("GelisPartisiLinesId");
            gvFabric.ChangeExpression("TotalAmount", "UnitPrice*TotalQty");
            gvFabric.MakeEditable("OwnerId", "GelisTarihi", "ProductId", "TotalQty", "Pcs", "TotalAmount");
            gvFabric.SetCaptions("GelisTarihi:Karbel Arrival Date");
            gvFabric.FormatAsNumber(2, "UnitPrice", "TotalAmount");

            gvFabric.AddSummary("TotalQty", "TotalAmount");
            gvFabric.Columns["OwnerId"].ColumnEdit = RepositoryManager.riCustomer;
            gvFabric.BestFitColumns();

            gvFabric.ShowingEditor += gvFabric_ShowingEditor;
        }

        void gvFabric_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = (gvFabric.GetFocusedRowCellValue("OwnerId").ToIntZero() != AppSession.UserInfo.CustomerId);
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvFabric.CloseEditor();
            gvFabric.UpdateCurrentRow();

            string xml = gvFabric.GetChangesAsXml("Table");
            if (xml != "")
            {
                ExecQuery("COUpdUnitPrice", xml);
                gvFabric.VeriyiGuncelle("COGetUnitPrice", AppSession.UserInfo.CustomerId);
            }
            else
                XtraMessageBox.Show("No changes !", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}